package com.ajx.blog.service.impl;

import com.ajx.blog.entity.CourseComment;
import com.ajx.blog.mapper.CourseCommentMapper;
import com.ajx.blog.mapper.CoursePersonMapper;
import com.ajx.blog.service.api.CourseCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CourseCommentServiceImpl extends ServiceImpl<CourseCommentMapper, CourseComment>
        implements CourseCommentService {

    @Autowired
    private CourseCommentMapper courseCommentMapper;
    @Autowired
    private CoursePersonMapper coursePersonMapper;

    // 根据课程id查询课程评论
    @Override
    public List<CourseComment> selectCourseCommentList(Integer cid) {
        QueryWrapper<CourseComment> qw = new QueryWrapper<>();
        qw.eq("cid", cid);
        return courseCommentMapper.selectList(qw);
    }

    // 添加评论
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean addCourseComment(CourseComment courseComment) {
        int row = courseCommentMapper.addCourseComment(courseComment);
        if (row > 0) {
            return true;
        }
        return false;
    }

    // 添加课程成功人数加1
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updatePersonNumber(Integer cid, Integer userid) {
        int row3 = coursePersonMapper.selectByCid(userid, cid);

        if (row3 > 0) {
            return false;
        } else {
            int row = courseCommentMapper.updatePersonNumber(cid);
            System.out.println(row + "--------------------------------------------------------------");

            int row2 = coursePersonMapper.addCoursePerson(userid, cid);
            System.out.println(row2 + "++++++++++++++++++++++++++++++++++++++++++++++++");

            if (row3 < 1 && row > 0 && row2 > 0) {
                return true;
            }
            return false;
        }

    }

}
